﻿using BYSJ.Web.Models;
using BYSJ.Web.Models.huDongCeBian;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BYSJ.IBLL;
using BYSJ.BLL;
using BYSJ.Model;
using BYSJ.Web.Models.shipin;
namespace BYSJ.Web.Filters
{
    /// <summary>
    /// 是否登录过滤
    /// </summary>
    public class AdminFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
           if(filterContext.HttpContext.Session["userSession"]==null)
           {
               filterContext.Result = new RedirectResult("/Logn/Index");
           }
        }
    }

    /// <summary>
    /// 顶部数据过滤类
    /// </summary>
    public class ShenFen : ActionFilterAttribute
    {
        IUserSetService UserServic = new UserSetService();
        IVideoShowSetService videoShow = new VideoShowSetService();
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult v = filterContext.Result as ViewResult;
            if (v != null)
            {
                baseModel bvm = v.Model as baseModel;
                bvm.name = Convert.ToString(filterContext.HttpContext.Session["userSession"]);
                var renshu = UserServic.LoadEntities(u => u.IsLock == false).ToList().Count();
                var videoinfo = videoShow.LoadEntities(u => u.id != null).ToList();
                bvm.renshu = renshu.ToString();
                bvm.videocount = videoinfo.Count().ToString();
                int sumindex = 0;
                foreach (var item in videoinfo)
                {
                    sumindex += item.videosize;
                }
                bvm.videotime = (sumindex / 1024 / 1024 / 3 / 60).ToString();
            }
        }
    }
    
    /// <summary>
    /// 学习明星过滤器
    /// </summary>

    public class start : ActionFilterAttribute
    {
        IvideojiluSetService jilu = new videojiluSetService();
        List<xuexistart> liststart = new List<xuexistart>();

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult v = filterContext.Result as ViewResult;
            if(v!=null)
            {
                shiPin bvm = v.Model as shiPin;
                int totalCount;
                //视频学习侧边栏数据
                var jiluresulr = jilu.LoadPageEntities(5, 1, out totalCount, u => u.id !=null, false, a => a.crettime).ToList();
                foreach (var item in jiluresulr)
                {
                    xuexistart start = new xuexistart();
                    start.name = item.UserSet.Name;
                    //item.VideoShow.name;得到每个视频的名字
                    start.content = item.VideoShowSet.name;
                    liststart.Add(start);
                }
                bvm.start = liststart;
            }
        }
    }
    /// <summary>
    /// 侧边栏过滤器
    /// </summary>
    public class huDongcebian:ActionFilterAttribute
    {
        IUserSetService userService = new UserSetService();
        IAnswerSetService answer = new AnswerSetService();
        IHuiFuSetService huifu = new HuiFuSetService();
        List<AnswerSet> dbanswerremen = new List<AnswerSet>();
        List<AnswerSet> dbanswertiwen = new List<AnswerSet>();
        List<HuiFuSet> dbanswerhuifu = new List<HuiFuSet>();
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult v = filterContext.Result as ViewResult;
            if (v != null)
            {
                CeBianJiHe bvm = v.Model as CeBianJiHe;
                string username = Convert.ToString(filterContext.HttpContext.Session["userSession"]);
                //查出来前5个热门提问
                int totalCount;
                dbanswerremen = answer.LoadPageEntities(5, 1, out totalCount, u => u.isdel == false, false, a => a.QuestingSet.Count).ToList();
                int tltalCounttiwen;
                dbanswertiwen = answer.LoadPageEntities(5, 1, out tltalCounttiwen, u => u.isdel == false&& u.UserSet.Name==username, false, a => a.creattime ).ToList();
                int tltalCounthuifu;
                dbanswerhuifu = huifu.LoadPageEntities(5, 1, out tltalCounthuifu, u => u.isdel == false && u.UserSet.Name == username,false,a=>a.creattime ).ToList();
                CeBianJiHe bv = new huDongcebian().CebianConvert(dbanswerremen, dbanswertiwen, dbanswerhuifu);
                bvm.huDongCeBian = bv.huDongCeBian;
                bvm.huDongCeBianHufu = bv.huDongCeBianHufu;
                bvm.huDongCeBianTiWen = bv.huDongCeBianTiWen;
                bvm.name = username;
            }
        }
        /// <summary>
        /// 整合侧边栏数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="dbanswerremen"></param>
        /// <param name="dbanswertiwen"></param>
        /// <param name="dbanswerhuifu"></param>
        /// <returns></returns>
        CeBianJiHe CebianConvert(List<AnswerSet> dbanswerremen, List<AnswerSet> dbanswertiwen, List<HuiFuSet> dbanswerhuifu)
        {
            CeBianJiHe cebianjihe=new CeBianJiHe();
            List<ReMen> cebianremen = new List<ReMen>();
            List<huDongMyTiWen> cebiantiwen = new List<huDongMyTiWen>();
            List<huDongMyHuiFu> cebianhuifu = new List<huDongMyHuiFu>();
             foreach (var item in dbanswerremen)
            {
                ReMen remen=new ReMen();
                if (item.title.Length > 10)
                {
                    remen.content = item.title.Substring(0, 10);
                }
                 else
                {
                    remen.content = item.title;
                }
                remen.id = item.id;
                cebianremen.Add(remen);
            }
             cebianjihe.huDongCeBian = cebianremen;
            foreach (var item in dbanswertiwen)
            {
                huDongMyTiWen tiwen=new huDongMyTiWen();
                if (item.title.Length > 10)
                {
                    tiwen.content = item.title.Substring(0, 10);
                }
                else
                {
                    tiwen.content = item.title;
                }
                tiwen.id = item.id;
                cebiantiwen.Add(tiwen);
            }
            cebianjihe.huDongCeBianTiWen = cebiantiwen;
            foreach (var item in dbanswerhuifu)
            {
                huDongMyHuiFu huifu = new huDongMyHuiFu();
                int weizhi = item.content.IndexOf(":");
                if (item.content.Substring(weizhi).Length > 10)
                {
                    huifu.content = item.content.Substring(weizhi, 10);
                }
                else
                {
                   huifu.content= item.content.Substring(weizhi+1);
                }
                huifu.id = item.QuestingSet.AnswerSet.id;
                cebianhuifu.Add(huifu);
            }
            cebianjihe.huDongCeBianHufu = cebianhuifu;
            return cebianjihe;
        }
    }
}